<?php
/**
 * Created by PhpStorm.
 * User: MG
 * Date: 2020/6/6
 * Time: 17:32
 */

namespace app\common\service;


class Common
{

    /**
     * 将数组按字母A-Z排序
     * @return [type] [description]
     */
    public function chartSort($list,$field='name')
    {
        $data = [];
        foreach ($list as $k => $v) {
            $v['letter'] = $this->getFirstChart($v[$field]);
            if (empty($data[$v['letter']])) {
                $data[$v['letter']] = [];
            }
            $data[$v['letter']][] = $v;
        }

        ksort($data);

        $i = 0;
        foreach ($data as $k => $v) {
//            $li[$i]['letter'] = $k;
            $li[$k] = $v;
            $i++;
        }

        return $li;
    }

    /**
     * 汉字首字母
     * @param $s0
     * @return string|null
     */
   private function getFirstChart($s0)
    {
        $fchar = ord(substr($s0, 0, 1));
        if (($fchar >= ord("a") and $fchar <= ord("z")) or ($fchar >= ord("A") and $fchar <= ord("Z"))) return strtoupper(chr($fchar));
        $s = iconv("UTF-8", "GBK", $s0);
        if (ord($s)>128) {
            $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
            if ($asc >= -20319 and $asc <= -20284) return "A";
            if ($asc >= -20283 and $asc <= -19776) return "B";
            if ($asc >= -19775 and $asc <= -19219) return "C";
            if ($asc >= -19218 and $asc <= -18711) return "D";
            if ($asc >= -18710 and $asc <= -18527) return "E";
            if ($asc >= -18526 and $asc <= -18240) return "F";
            if ($asc >= -18239 and $asc <= -17923) return "G";
            if ($asc >= -17922 and $asc <= -17418) return "H";
            if ($asc >= -17417 and $asc <= -16475) return "J";
            if ($asc >= -16474 and $asc <= -16213) return "K";
            if ($asc >= -16212 and $asc <= -15641) return "L";
            if ($asc >= -15640 and $asc <= -15166) return "M";
            if ($asc >= -15165 and $asc <= -14923) return "N";
            if ($asc >= -14922 and $asc <= -14915) return "O";
            if ($asc >= -14914 and $asc <= -14631) return "P";
            if ($asc >= -14630 and $asc <= -14150) return "Q";
            if ($asc >= -14149 and $asc <= -14091) return "R";
            if ($asc >= -14090 and $asc <= -13319) return "S";
            if ($asc >= -13318 and $asc <= -12839) return "T";
            if ($asc >= -12838 and $asc <= -12557) return "W";
            if ($asc >= -12556 and $asc <= -11848) return "X";
            if ($asc >= -11847 and $asc <= -11056) return "Y";
            if ($asc >= -11055 and $asc <= -10247) return "Z";
        }elseif (ord($s)>=48 and ord($s)<=57){
            switch(iconv_substr($s,0,1,'utf-8')) {
                case 1:
                    return "Y";
                case 2:
                    return "E";
                case 3:
                    return "S";
                case 4:
                    return "S";
                case 5:
                    return "W";
                case 6:
                    return "L";
                case 7:
                    return "Q";
                case 8:
                    return "B";
                case 9:
                    return "J";
                case 0:
                    return "L";
            }
        }else if(ord($s)>=65 and ord($s)<=90){ //大写英文开头
            return substr($s,0,1);
        }else if(ord($s)>=97 and ord($s)<=122){ //小写英文开头
            return strtoupper(substr($s,0,1));
        }else {
            return iconv_substr($s0,0,1,'utf-8');
        }

    }


    /**
     * 替换手机号 银行卡号为***
     * @param $str
     * @param int $startlen 开始长度  默认为4
     * @param int $endlen 结束长度 默认为3
     * @return null|string|string[]
     */
    public function strreplace($str , $startlen = 4 , $endlen = 3)
    {
        $repstr="";
        if (strlen($str)< ($startlen + $endlen+1)){
            return $str;
        }
        $count = strlen($str) - $startlen - $endlen;
        for ($i = 0; $i < $count; $i++) {
            $repstr.="*";
        }
        return preg_replace('/(\d{' . $startlen . '})\d+(\d{' . $endlen . '})/', '${1}' . $repstr . '${2}', $str);
    }

    /**
     * 创建自定义token
     */
    public function createToken($name)
    {
        $time=time();
        $info=$name.'.'.$time.'.yongjiu'.md5(uniqid());
        $signature=hash_hmac('md5',$info,'youpin');
        return $signature;
    }

    /**
     * 验证自定义token'
     */
    public function checkToken($token)
    {
        $explode=explode('.',$token);
        if (!empty($explode[0]) && !empty($explode[1]) && !empty($explode[2]) && !empty($explode[3])){
            $info=$explode[0].'.'.$explode[1].'.'.$explode[2];
            $true_signature=hash_hmac('md5',$info,'youpin');
            if ($true_signature==$explode[3]){
                print_r('合法');
            }
        }
    }

    /**
     * post请求
     */
    public function request_post($url = '', $post_data = array()) {
        if (empty($url) || empty($post_data)) {
            return false;
        }

        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $postUrl = $url;
        $curlPost = $post_data;

        header("Content-type: application/json");
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }

    public function CurlRequest($url,$data=null,$header=null)
    {
        //初始化浏览器
        $ch = curl_init();
        //设置浏览器，把参数url传到浏览器的设置当中
        curl_setopt($ch, CURLOPT_URL, $url);
        //以字符串形式返回到浏览器当中
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //禁止https协议验证域名，0就是禁止验证域名且兼容php5.6
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        //禁止https协议验证ssl安全认证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //判断data是否有数据，如果有data数据传入那么就把curl的请求方式设置为POST请求方式
        if ( !empty($data) ) {
            //设置POST请求方式
            @curl_setopt($ch, CURLOPT_POST, true);
            //设置POST的数据包
            @curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //设置header头
        if ( !empty($header) ) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        //让curl发起请求
        $str = curl_exec($ch);
        //关闭curl浏览器
        curl_close($ch);
        //把请求回来的数据返回
        return $str;
    }

}